package com.wohuijituan.whcsmerchant.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.viewpagerindicator.TabPageIndicator;
import com.wohuijituan.whcsmerchant.R;
import com.wohuijituan.whcsmerchant.fragment.GoodsAddFragment;
import com.wohuijituan.whcsmerchant.fragment.GoodsSellFragment;
import com.wohuijituan.whcsmerchant.utils.ExtraName;

import java.util.ArrayList;
import java.util.List;

public class GoodsManageActivity extends MyBaseActivity implements View.OnClickListener{

    /**
     * Tab标题
     */
    private static final String[] TITLE = new String[] {"出售中","已下架","去添加"};
    private List<Fragment> fragmentList;
    private TabPageIndicator indicator;
    private ViewPager pager;
    private String categoryName;//传入参数,商品分类名称
    private String categoryNumber;//传入数据,商品分类GUID
    private ImageButton titleGoBack;
    private TextView titleAction;
    private TextView titleName;
    private ImageButton titleIbAction;
    private Intent intent;

    private void assignViews() {
        titleGoBack = (ImageButton) findViewById(R.id.title_go_back);
        titleAction = (TextView) findViewById(R.id.title_tv_action);
        indicator = (TabPageIndicator) findViewById(R.id.indicator_goods_manager);
        pager = (ViewPager) findViewById(R.id.pager_goods_manager);
        titleName = (TextView) findViewById(R.id.title_name);
        titleIbAction = (ImageButton) findViewById(R.id.title_ib_action);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_goods);
        assignViews();
        initData();
        initView();
        initListener();
    }



    private void initData() {
        intent = getIntent();
        categoryName = intent.getStringExtra(ExtraName.CATEGORY_NAME);
        categoryNumber = intent.getStringExtra(ExtraName.CATEGORY_NUMBER);

    }

    private void initView() {
        if (categoryName==null||"".equals(categoryName)){
            titleName.setText("商品列表");
        }else {
            titleName.setText(categoryName);
        }
        titleGoBack.setVisibility(View.VISIBLE);
        titleIbAction.setVisibility(View.VISIBLE);
        titleIbAction.setImageDrawable(getResources().getDrawable(R.drawable.icon_search));
        titleGoBack.setOnClickListener(this);
        fragmentList = new ArrayList<>();
        fragmentList.add(GoodsSellFragment.newInstance(GoodsSellFragment.STYLE_ONSELL));
        fragmentList.add(GoodsSellFragment.newInstance(GoodsSellFragment.STYLE_UNSELL));
        fragmentList.add(new GoodsAddFragment());
        FragmentPagerAdapter adapter = new TabPageIndicatorAdapter(
                getSupportFragmentManager(),fragmentList);
        pager.setAdapter(adapter);
        indicator.setViewPager(pager,0);
    }
    private void initListener() {
        titleGoBack.setOnClickListener(this);
        titleIbAction.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_go_back:
                onBackPressed();
                break;
            case R.id.title_ib_action:
                intent.setClass(ctx,SystemGoodsSearchActivity.class);
                startActivity(intent);
                break;
        }
    }


    /**
     * ViewPager适配器
     * @author len
     */
    class TabPageIndicatorAdapter extends FragmentPagerAdapter {
        List<Fragment> list;
        public TabPageIndicatorAdapter(FragmentManager fm ,List<Fragment> dataList) {
            super(fm);
            list = dataList;
        }

        @Override
        public Fragment getItem(int position) {

            return list.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLE[position % TITLE.length];
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
    public String getCategoryNumber() {
        return categoryNumber;
    }

    public void setCategoryNumber(String categoryNumber) {
        this.categoryNumber = categoryNumber;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }
}
